﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using CSEdit.Server.Models;
using System.IO;
using System.Text;
using System.Text.Encodings.Web;
using System.Net;
using System.Runtime.InteropServices;

namespace CSEdit.Server.Controllers
{
    public class AdminController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }
        public IActionResult GetNode(string fname)
        {
            List<ZNode> r = new List<ZNode>();
            if (string.IsNullOrEmpty(fname))
            {
                string[] root;
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                    root = Directory.GetDirectories("/");
                else
                    root = Directory.GetLogicalDrives();//.GetDirectories(b);
                foreach (var item in root)
                {
                    DirectoryInfo dinfo = new DirectoryInfo(item);
                    ZNode node = new ZNode();
                    node.Fname = WebUtility.UrlEncode(item);
                    node.Name = dinfo.Name;
                    node.IsParent = true;
                    r.Add(node);
                }
            }
            else
            {
                string path = WebUtility.UrlDecode(fname);
                if (Directory.Exists(path))//目录
                {
                    string[] dirs = Directory.GetDirectories(path);
                    foreach (var item in dirs)
                    {
                        DirectoryInfo dinfo = new DirectoryInfo(item);
                        ZNode node = new ZNode();
                        node.Fname = WebUtility.UrlEncode(item);
                        node.Name = dinfo.Name;
                        node.IsParent = true;
                        r.Add(node);
                    }
                    string[] files = Directory.GetFiles(path);
                    foreach (var item in files)
                    {
                        DirectoryInfo dinfo = new DirectoryInfo(item);
                        ZNode node = new ZNode();
                        node.Fname = WebUtility.UrlEncode(item);
                        node.Name = dinfo.Name;
                        node.IsParent = false;
                        r.Add(node);
                    }
                }
            }
            return Json(r);
        }
        public  IActionResult Editor(string path)
        {
            //path = WebUtility.UrlDecode(path);
            ViewBag.Path = WebUtility.UrlEncode(path);
            //string realPath = path = WebUtility.UrlDecode(path);
            //string r = string.Empty;
            //using (var sr = new StreamReader(realPath, encoding: Encoding.UTF8))
            //{
            //    r = await sr.ReadToEndAsync();
            //}
            //ViewBag.Data = r;
            return View();
        }
        public async Task<IActionResult> GetFile(string path)
        {
            path = WebUtility.UrlDecode(path);//.HtmlDecode()
            //HtmlEncoder.Default.Encode(path,).Decode(code)
            //path = Uri.EscapeDataString(path);
            //path = "d:\\1.txt";
            string r = string.Empty;
            using (var sr = new StreamReader(path, encoding: Encoding.UTF8))
            {
                r = await sr.ReadToEndAsync();
            }
            return Json(new { ret = 0, data = r });
        }
        public async Task<IActionResult> PostFile(string name, string data)
        {
            try
            {
                string path = WebUtility.UrlDecode(name);
                FileInfo file = new FileInfo(path);
                using (StreamWriter sw = new StreamWriter(path, false, encoding: Encoding.UTF8))
                {
                    await sw.WriteAsync(data);
                }
                return Json(new { ret = 0 });
            }
            catch (Exception ex)
            {
                return Json(new { ret = 1, ex = ex.ToString() });
            }
        }
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}
